package com.lwp.springboot.jpa.service;
import java.sql.Timestamp;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.lwp.springboot.jpa.entity.LotLotteryEntity;
import com.lwp.springboot.jpa.repository.LotLotteryRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class KjService {

    @Autowired
    LotLotteryRepository lotLotteryRepository;

    /**
     * 添加随机开奖记录
     * <p>
     *     先删除所有数据，之后添加
     * </p>
     * @param num
     */
    @Transactional
    public void addRandomRecords(int num) {
        if (num <= 0) {
            return ;
        }
        long startId = 1;
        int period = 1;
        List<LotLotteryEntity> all = lotLotteryRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
        if (CollectionUtils.isNotEmpty(all)) {
            LotLotteryEntity lotLotteryEntity = all.get(0);
            startId = lotLotteryEntity.getId();
            period = lotLotteryEntity.getPeriod() == null ? 1 : lotLotteryEntity.getPeriod();
        }
        List<LotLotteryEntity> entities = Lists.newArrayList();
        for (int i = 0; i < num; i++) {
            LotLotteryEntity entity = new LotLotteryEntity();
            int addStep = ( i);
            entity.setId(startId + addStep);
//            entity.setName();
            entity.setPeriod(period + addStep);
//            entity.setType((byte)0);
//            entity.setBall1((byte)0);
//            entity.setBall2((byte)0);
//            entity.setBall3((byte)0);
//            entity.setBall4((byte)0);
//            entity.setBall5((byte)0);
//            entity.setBall6((byte)0);
//            entity.setBall7((byte)0);
            // 设置中奖号码
            randNumForEntity(entity);
            entity.setRemark("");
            entity.setCreateDate(new Timestamp(new java.util.Date().getTime()));
            entity.setUpdateDate(new Timestamp(new java.util.Date().getTime()));
//            entity.setStatus((byte)0);
            entity.setStatus((byte)1);
            //
            Date curDate = DateUtils.addDays(new Date(), 0 + i - num);
            Date nextKjDate = DateUtils.addDays(curDate, 1);
            entity.setYear(Integer.parseInt(DateFormatUtils.format(curDate, "YYYYY")));
            entity.setNextKjDatetime(new Timestamp(nextKjDate.getTime()));
            entity.setNextPeriod(period + addStep + 1);
            entity.setKjDatetime(new Timestamp(curDate.getTime()));
            entity.setKjStatus((byte)1);
            entity.setCreateBy(1);
            entity.setUpdateBy(1);

            entities.add(entity);
        }
        lotLotteryRepository.saveAll(entities);
    }

    private static void randNumForEntity(LotLotteryEntity entity) {
        List<String> list = Lists.newArrayList();
        ArrayList<Byte> strings = Lists.newArrayList(bollNums);
        Byte ball1 = delAndRetrunVal(strings);
        Byte ball2 = delAndRetrunVal(strings);
        Byte ball3 = delAndRetrunVal(strings);
        Byte ball4 = delAndRetrunVal(strings);
        Byte ball5 = delAndRetrunVal(strings);
        Byte ball6 = delAndRetrunVal(strings);
        Byte ball7 = delAndRetrunVal(strings);
        entity.setBall1(ball1);
        entity.setBall2(ball2);
        entity.setBall3(ball3);
        entity.setBall4(ball4);
        entity.setBall5(ball5);
        entity.setBall6(ball6);
        entity.setBall7(ball7);
    }

    private static Byte delAndRetrunVal(ArrayList<Byte> strings) {
        int index = RandomUtils.nextInt(0, strings.size());
        Byte s = strings.get(index);
        strings.remove(index);
        return s;
    }
//    private static final String[] bollNums = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49"};
    private static final Byte[] bollNums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49};

    public static void main(String[] args) {
        StringBuilder str = new StringBuilder();
        for (int i = 1; i < 50; i++) {
            /*str.append("\"");
            if (i < 10) {
                str.append("0" + i);
            } else {
                str.append(i);
            }
            str.append("\",");*/
            str.append("");
            if (i < 10) {
                str.append(i);
            } else {
                str.append(i);
            }
            str.append(",");
        }
        System.out.println(str.toString());
        randNumForEntity(new LotLotteryEntity());
    }
}
